Dynamic Strategy Pattern [migrated]
Posted
by
Karl Barker
on Programmers
See other posts from Programmers
or by Karl Barker
Published on 2012-12-08T00:04:52Z
Indexed on
2012/12/08
11:35 UTC
Read the original article
Hit count: 204
java
|design-patterns
So I'm writing a web service architecture which includes FunctionProvider
classes which do the actual processing of requests, and a main Endpoint
class which receives and delegates requests to the proper FunctionProvider
.
I don't know exactly the FunctionProvider
s available at runtime, so I need to be able to 'register' (if that's the right word) them with my main Endpoint
class, and query them to see if they match an incoming request.
public class MyFunc implements FunctionProvider{
static {
MyEndpoint.register(MyFunc);
}
public Boolean matchesRequest(Request req){...}
public void processRequest(Request req){...}
}
public class MyEndpoint{
private static ArrayList<FunctionProvider> functions = new ArrayList<FunctionProvider>();
public void register(Class clz){
functions.add(clz);
}
public void doPost(Request request){
//find the FunctionProvider in functions
//matching the request
}
}
I've really not done much reflective Java like this (and the above is likely wrong, but hopefully demonstrates my intentions).
What's the nicest way to implement this without getting hacky?
© Programmers or respective owner